﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using Newtonsoft.Json;

namespace Controllers
{
    public class VehicleController : BaseController
    {
        //分页查询
        public ActionResult Find(int start, int limit,string idStr)
        {
            int empId;
            IList<int> deptIdList = getAuthedDeptIds("Vehicle", idStr, out empId);
            EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;

            IQueryable<VehicleDTO> list = from r in ctx.ReqVehicleMains
                                          //where (from r1 in ctx.EmpDepPositions where deptIdList.ToArray().Contains(r1.DeptId) select r1.EmpId).Contains(r.EmpId)
                                         join s in ctx.SignFlows on
                                             new { r.FormNo, IsCurrent = 1 } equals new { s.FormNo, s.IsCurrent }
                                             into t
                                         from x in t.DefaultIfEmpty()
                                         where r.StatusId!=0 || r.EmpId==ebi.Id
                                         orderby r.FormNo descending
                                          select new VehicleDTO
                                         {
                                             FormNo = r.FormNo,
                                             EmpId = r.EmpId,
                                             EmpName = r.Employee.Name,
                                             PreStartTime = r.PreStartTime,
                                             PreEndTime = r.PreEndTime,
                                             Vehicle = r.Vehicle,
                                             Cause = r.Cause,
                                             StatusId = r.StatusId,
                                             CheckEmpId = x.CheckEmpId
                                         };
            if(empId!=0)
            {
                list = list.Where(r =>r.EmpId==empId);
            }
            else
            {
                list = list.Where(r => (from r1 in ctx.EmpDepPositions where deptIdList.ToArray().Contains(r1.DeptId) select r1.EmpId).Contains(r.EmpId));
            }
            //IQueryable<ReqVehicleMains> list = from r in ctx.ReqVehicleMains select r;
            CJson CJson = new CJson();
            string json = CJson.ToJsonString("totalCount:" + list.Count() + ",data", list.Skip(start).Take(limit).ToList());
            return Content(json);
        }

        //删除明细
        public string DelDetail(int id)
        {
            string retStr = "Succeed";
            ReqVehicleSubs oriObj = ctx.ReqVehicleSubs.Single(r => r.Id == id);
            ctx.ReqVehicleSubs.DeleteOnSubmit(oriObj);
            try
            {
                ctx.SubmitChanges();
            }
            catch (Exception ex)
            {
                retStr = ex.Message;
            }
            return retStr;
        }

        //删除申请,含明细
        public ActionResult Delete(string formNo)
        {
            var rdto = new ResultMessages();
            ReqVehicleMains oriObj = ctx.ReqVehicleMains.Single(r => r.FormNo ==formNo);
            ctx.ReqVehicleSubs.DeleteAllOnSubmit(oriObj.ReqVehicleSubs);
            ctx.ReqVehicleMains.DeleteOnSubmit(oriObj);
            try
            {
                ctx.SubmitChanges();
                rdto.Message = "删除成功";
                rdto.Result = true;
            }
            catch
            {
                rdto.Message = "删除失败";
            }
            return Json(rdto);
        }

        //申请销单(司机才能销单,用车申请不需审核)
        public ActionResult Logout(string json)
        {
            var rdto = new ResultMessages();
            rdto.Message = "";
            JavaScriptSerializer jss = new JavaScriptSerializer();
            ReqVehicleMains obj = jss.Deserialize<ReqVehicleMains>(json);
            try
            {
                ReqVehicleMains oriObj = ctx.ReqVehicleMains.Single(r => r.FormNo == obj.FormNo);
                oriObj.RealStartTime = obj.RealStartTime;
                oriObj.RealEndTime = obj.RealEndTime;
                oriObj.OilFee = obj.OilFee;
                oriObj.roadFee = obj.roadFee;
                oriObj.ParkingFee = obj.ParkingFee;
                oriObj.OtherFee = obj.OtherFee;
                oriObj.Remarks = obj.Remarks;
                oriObj.StatusId = obj.StatusId;

                ctx.SubmitChanges();
                rdto.Message = "更新成功";
                rdto.Result = true;
            }
            catch (Exception ex)
            {
                rdto.Message = "更新失败";
                rdto.Result = false;
            }
            return Json(rdto);
        }
    }
}